package view.billing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import view.controls.ObservingTextField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.qt.datapicker.DatePicker;

import controller.billing.GuiCreateBillViewController;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CreateBillView extends javax.swing.JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4915724691137324858L;
	private JTable tableReservations;
	private JPanel reservationPanel;
	private ObservingTextField txtDeparture;
	private ObservingTextField txtArrival;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JPanel pnlSearchReservations;
	private JLabel lblDepartureDate;
	private ObservingTextField txtDate;
	private JButton btnCreateBill;
	private JPanel pnlBottom;
	private JLabel lblReservations;
	private JLabel jLabel3;
	private JTextField txtCustomer;
	private JPanel jPanel4;
	private JPanel jPanel3;
	private JLabel jLabel6;
	private JComboBox cbxCustomer;
	private JLabel jLabel4;
	private JButton btnClearDeparture;
	private JLabel jLabel5;
	private JCheckBox checkShowUnpayedOnly;
	private JTextField txtRoom;
	private JButton btnClearRoom;
	private JPanel jPanel2;
	private JButton btnClearCustomer;
	private JButton btnClearArrival;
	private JPanel jPanel1;
	private JButton btnDeleteReservation;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
		
	public CreateBillView() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(543, 337));
			{
				jPanel1 = new JPanel();
				BorderLayout jPanel1Layout = new BorderLayout();
				jPanel1.setLayout(jPanel1Layout);
				this.add(jPanel1, BorderLayout.NORTH);
				jPanel1.setPreferredSize(new java.awt.Dimension(440, 107));
				jPanel1.setBorder(BorderFactory.createTitledBorder(""));
				{
					pnlSearchReservations = new JPanel();
					jPanel1.add(pnlSearchReservations, BorderLayout.CENTER);
					FormLayout pnlSearchReservationsLayout = new FormLayout(
							"41dlu, 55dlu, 13dlu, 50dlu, 71dlu, 13dlu", 
							"max(p;5dlu), max(p;5dlu), 13dlu");
					pnlSearchReservations.setLayout(pnlSearchReservationsLayout);
					pnlSearchReservations.setPreferredSize(new java.awt.Dimension(430, 58));
					{
						jLabel1 = new JLabel();
						pnlSearchReservations.add(jLabel1, new CellConstraints(1, 1, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 5)));
						jLabel1.setText("From Date");
						jLabel1.setLayout(null);
					}
					{
						jLabel2 = new JLabel();
						pnlSearchReservations.add(jLabel2, new CellConstraints(1, 2, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 5)));
						jLabel2.setText("Until Date");
						jLabel2.setLayout(null);
					}
					{
						txtArrival = new ObservingTextField();
						pnlSearchReservations.add(txtArrival, new CellConstraints(2, 1, 1, 1, CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(2, 0, 2, 0)));
						addDatePicker(txtArrival);
					}
					{
						txtDeparture = new ObservingTextField();
						pnlSearchReservations.add(txtDeparture, new CellConstraints(2, 2, 1, 1, CellConstraints.FILL, CellConstraints.FILL, new Insets(2, 0, 2, 0)));
						addDatePicker(txtDeparture);
					}
					{
						jLabel3 = new JLabel();
						pnlSearchReservations.add(jLabel3, new CellConstraints(4, 1, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 5)));
						jLabel3.setText("Customer");
						jLabel3.setLayout(null);
					}
					{
						txtCustomer = new JTextField();
						pnlSearchReservations.add(txtCustomer, new CellConstraints(5, 1, 1, 1, CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(2, 0, 2, 0)));
					}
					{
						btnClearArrival = new JButton();
						pnlSearchReservations.add(btnClearArrival, new CellConstraints(3, 1, 1, 1, CellConstraints.FILL, CellConstraints.DEFAULT, new Insets(3, 0, 3, 0)));
						btnClearArrival.setLayout(null);
						btnClearArrival.setText("x");
						btnClearArrival.setMargin(new java.awt.Insets(1, 1, 1, 1));
					}
					{
						btnClearDeparture = new JButton();
						pnlSearchReservations.add(btnClearDeparture, new CellConstraints(3, 2, 1, 1, CellConstraints.FILL, CellConstraints.DEFAULT, new Insets(3, 0, 3, 0)));
						btnClearDeparture.setLayout(null);
						btnClearDeparture.setText("x");
						btnClearDeparture.setMargin(new java.awt.Insets(1, 1, 1, 1));
					}
					{
						btnClearCustomer = new JButton();
						pnlSearchReservations.add(btnClearCustomer, new CellConstraints(6, 1, 1, 1, CellConstraints.FILL, CellConstraints.DEFAULT, new Insets(3, 0, 3, 0)));
						btnClearCustomer.setLayout(null);
						btnClearCustomer.setText("x");
						btnClearCustomer.setMargin(new java.awt.Insets(1, 1, 1, 1));
					}
					{
						jLabel5 = new JLabel();
						BorderLayout jLabel5Layout = new BorderLayout();
						pnlSearchReservations.add(jLabel5, new CellConstraints(4, 2, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 5)));
						jLabel5.setText("Room");
						jLabel5.setLayout(null);
					}
					{
						btnClearRoom = new JButton();
						pnlSearchReservations.add(btnClearRoom, new CellConstraints(6, 2, 1, 1, CellConstraints.FILL, CellConstraints.DEFAULT, new Insets(3, 0, 3, 0)));
						btnClearRoom.setLayout(null);
						btnClearRoom.setText("x");
						btnClearRoom.setMargin(new java.awt.Insets(1,1,1,1));
					}
					{
						txtRoom = new JTextField();
						pnlSearchReservations.add(txtRoom, new CellConstraints(5, 2, 1, 1, CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(2, 0, 2, 0)));
					}
					{
						checkShowUnpayedOnly = new JCheckBox();
						pnlSearchReservations.add(checkShowUnpayedOnly, new CellConstraints(4, 3, 3, 1, CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 3)));
						checkShowUnpayedOnly.setLayout(null);
						checkShowUnpayedOnly.setText("Show unpayed Reservations only");
						checkShowUnpayedOnly.setHorizontalTextPosition(SwingConstants.LEFT);
						checkShowUnpayedOnly.setHorizontalAlignment(SwingConstants.RIGHT);
					}
				}
				{
					jLabel4 = new JLabel();
					jPanel1.add(jLabel4, BorderLayout.NORTH);
					jLabel4.setText("Search Criteria");
					jLabel4.setLayout(null);
					jLabel4.setPreferredSize(new java.awt.Dimension(430, 22));
					jLabel4.setVerticalAlignment(SwingConstants.TOP);
				}
			}
			{
				reservationPanel = new JPanel();
				BorderLayout reservationPanelLayout = new BorderLayout();
				reservationPanel.setLayout(reservationPanelLayout);
				this.add(reservationPanel, BorderLayout.CENTER);
				reservationPanel.setPreferredSize(new java.awt.Dimension(448, 291));
				reservationPanel.setBorder(BorderFactory.createTitledBorder(""));
				{
					lblReservations = new JLabel();
					BorderLayout lblReservationsLayout = new BorderLayout();
					lblReservations.setLayout(lblReservationsLayout);
					reservationPanel.add(lblReservations, BorderLayout.NORTH);
					lblReservations.setText("Reservations");
					lblReservations.setPreferredSize(new java.awt.Dimension(430, 33));
					lblReservations.setVerticalAlignment(SwingConstants.TOP);
				}
				{
					jPanel2 = new JPanel();
					BorderLayout jPanel2Layout = new BorderLayout();
					jPanel2.setLayout(jPanel2Layout);
					reservationPanel.add(jPanel2, BorderLayout.CENTER);
					jPanel2.setPreferredSize(new java.awt.Dimension(430, 166));
					{
						TableModel tableReservationsModel = 
							new DefaultTableModel(
									new String[][] { { "One", "Two" }, { "Three", "Four" } },
									new String[] { "Column 1", "Column 2" });
						tableReservations = new JTable();
						jPanel2.add(tableReservations, BorderLayout.CENTER);
						tableReservations.setModel(tableReservationsModel);
						tableReservations.setLayout(null);
						tableReservations.setPreferredSize(new java.awt.Dimension(448, 235));
						tableReservations.getTableHeader().setBounds(0, 0, 430, 171);
						reservationPanel.add(tableReservations.getTableHeader() ,BorderLayout.PAGE_START);
					}
				}
			}
			{
				pnlBottom = new JPanel();
				this.add(pnlBottom, BorderLayout.SOUTH);
				FlowLayout btnCreateBillLayout = new FlowLayout();
				pnlBottom.setLayout(btnCreateBillLayout);
				pnlBottom.setPreferredSize(new java.awt.Dimension(543, 70));
				pnlBottom.setBorder(BorderFactory.createTitledBorder(""));
				{
					jPanel3 = new JPanel();
					BorderLayout jPanel3Layout = new BorderLayout();
					jPanel3.setLayout(jPanel3Layout);
					pnlBottom.add(jPanel3);
					jPanel3.setPreferredSize(new java.awt.Dimension(282, 22));
					{
						ComboBoxModel cbxCustomerModel = 
							new DefaultComboBoxModel(
									new String[] { "Choose paying Customer" });
						cbxCustomer = new JComboBox();
						jPanel3.add(cbxCustomer, BorderLayout.CENTER);
						cbxCustomer.setModel(cbxCustomerModel);
						cbxCustomer.setRenderer(new CustomerComboBoxRenderer());
						cbxCustomer.setPreferredSize(new java.awt.Dimension(84, 22));
					}
					{
						jLabel6 = new JLabel();
						jPanel3.add(jLabel6, BorderLayout.WEST);
						BorderLayout jLabel6Layout = new BorderLayout();
						jLabel6.setText("Paying Customer");
						jLabel6.setLayout(null);
					}
				}
				{
					jPanel4 = new JPanel();
					BorderLayout jPanel4Layout = new BorderLayout();
					jPanel4.setLayout(jPanel4Layout);
					pnlBottom.add(jPanel4);
					jPanel4.setPreferredSize(new java.awt.Dimension(231, 22));
					{
						lblDepartureDate = new JLabel();
						jPanel4.add(lblDepartureDate, BorderLayout.WEST);
						lblDepartureDate.setText("Date of Departure");
						lblDepartureDate.setLayout(null);
						lblDepartureDate.setPreferredSize(new java.awt.Dimension(114, 22));
					}
					{
						txtDate = new ObservingTextField();
						jPanel4.add(txtDate, BorderLayout.CENTER);

						// set current date
						DatePicker dp = new DatePicker(txtDate);
						txtDate.setText(dp.formatDate(new Date()));
						
						txtDate.setEditable(false);
						new DatePicker(txtDate);
						txtDate.setPreferredSize(new java.awt.Dimension(90, 22));
						addDatePicker(txtDate);
					}
				}
				{
					btnCreateBill = new JButton();
					pnlBottom.add(btnCreateBill);
					btnCreateBill.setLayout(btnCreateBillLayout);
					btnCreateBill.setText("Create Bill");
					btnCreateBill.setPreferredSize(new java.awt.Dimension(130, 23));
				}
				{
					btnDeleteReservation = new JButton();
					pnlBottom.add(btnDeleteReservation);
					btnDeleteReservation.setLayout(btnCreateBillLayout);
					btnDeleteReservation.setText("Delete Reservation");
					btnDeleteReservation.setPreferredSize(new java.awt.Dimension(190, 23));
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public JCheckBox getCheckShowUnpayedOnly() {
		return checkShowUnpayedOnly;
	}

	public JTextField getTxtRoom() {
		return txtRoom;
	}

	private void addDatePicker(final ObservingTextField textField){
		textField.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				 // instantiate the DatePicker
                DatePicker dp = new DatePicker(textField);
                // previously selected date
                Date selectedDate = dp.parseDate(textField.getText());
                dp.setSelectedDate(selectedDate);
                dp.start(textField);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public ObservingTextField getTxtDeparture() {
		return txtDeparture;
	}

	public ObservingTextField getTxtArrival() {
		return txtArrival;
	}

	public ObservingTextField getTxtDate() {
		return txtDate;
	}

	public JButton getBtnCreateBill() {
		return btnCreateBill;
	}

	public JButton getBtnClearDeparture() {
		return btnClearDeparture;
	}

	public JButton getBtnClearCustomer() {
		return btnClearCustomer;
	}

	public JButton getBtnClearArrival() {
		return btnClearArrival;
	}

	public JTable getTableReservations() {
		return tableReservations;
	}
	
	public Date getDateOfDepartureForBill(){
		 DatePicker dp = new DatePicker(txtDate);
         // previously selected date
        return dp.parseDate(txtDate.getText());
	}
	
	public JTextField getTxtCustomer() {
		return txtCustomer;
	}
	
	
	public JButton getBtnDeleteReservation() {
		return btnDeleteReservation;
	}

	public void setBtnDeleteReservation(JButton btnDeleteReservation) {
		this.btnDeleteReservation = btnDeleteReservation;
	}

	public void addEventListeners(GuiCreateBillViewController c){
		this.btnClearArrival.addActionListener(c);
		this.btnClearCustomer.addActionListener(c);
		this.btnClearDeparture.addActionListener(c);
		this.btnCreateBill.addActionListener(c);
		this.btnClearRoom.addActionListener(c);
		this.btnDeleteReservation.addActionListener(c);
		
		this.txtArrival.getDocument().addDocumentListener(c);
		this.txtDeparture.getDocument().addDocumentListener(c);
		this.txtCustomer.getDocument().addDocumentListener(c);
		this.txtRoom.getDocument().addDocumentListener(c);
		
		this.checkShowUnpayedOnly.addItemListener(c);
		
		this.tableReservations.getSelectionModel().addListSelectionListener(c);
	}
	
	public JComboBox getCbxCustomer() {
		return cbxCustomer;
	}

}
